/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.fafaldo.fabtoolbar.sample.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;

import com.github.fafaldo.fabtoolbar.component.FABToolbarLayout;
import com.github.fafaldo.fabtoolbar.sample.ResourceTable;
import com.github.fafaldo.fabtoolbar.sample.adapter.ContentProvider;
import com.muddzdev.styleabletoast.styleabletoast.StyleableToast;

/**
 * MainAbilitySlice
 *
 * @since 2021-07-13
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private final int CORNER_RADIUS = 19;
    private final int TEXT_SIZE = 19;
    private final int COLOR_ALPHA = 90;
    private final int COLOR_RED = 243;
    private final int COLOR_GREEN = 240;
    private final int COLOR_BLUE = 240;
    private final int ITEM_NUMBER = 30;
    private Image fabToolbarFab;
    private FABToolbarLayout fabToolbarLayout;
    private DirectionalLayout fabToolbarToolbar;
    private ListContainer listContainer;
    private DependentLayout fabToolbarContainer;
    private Image imageOne;
    private Image imageTwo;
    private Image imageThree;
    private Image imageFour;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initComponent();
        initListener();
    }

    private void initComponent() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        listContainer.setItemProvider(new ContentProvider(this, ITEM_NUMBER));
        fabToolbarLayout = (FABToolbarLayout) findComponentById(ResourceTable.Id_fab_toolbar);

        fabToolbarContainer = (DependentLayout) findComponentById(ResourceTable.Id_fab_toolbar_container);
        fabToolbarFab = (Image) findComponentById(ResourceTable.Id_fab_toolbar_fab);
        fabToolbarToolbar = (DirectionalLayout) findComponentById(ResourceTable.Id_fab_toolbar_toolbar);
        imageOne = (Image) findComponentById(ResourceTable.Id_one);
        imageTwo = (Image) findComponentById(ResourceTable.Id_two);
        imageThree = (Image) findComponentById(ResourceTable.Id_three);
        imageFour = (Image) findComponentById(ResourceTable.Id_four);

        imageOne.setClickedListener(this);
        imageTwo.setClickedListener(this);
        imageThree.setClickedListener(this);
        imageFour.setClickedListener(this);

        fabToolbarLayout.setFabToolbarContainer(fabToolbarContainer);
        fabToolbarLayout.setFabToolbarFab(fabToolbarFab);
        fabToolbarLayout.setFabToolbarToolbar(fabToolbarToolbar);
    }

    private void initListener() {
        fabToolbarFab.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                fabToolbarLayout.show();
            }
        });

        fabToolbarFab.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                fabToolbarLayout.show();
            }
        });

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long lo) {
                fabToolbarLayout.show();
            }
        });

        listContainer.setItemLongClickedListener(new ListContainer.ItemLongClickedListener() {
            @Override
            public boolean onItemLongClicked(ListContainer listContainer, Component component, int position, long lo) {
                fabToolbarLayout.show();
                return false;
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        if (fabToolbarToolbar.getVisibility() == Component.INVISIBLE) {
            super.onBackPressed();
        } else {
            fabToolbarLayout.hide();
        }
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_one:
            case ResourceTable.Id_two:
            case ResourceTable.Id_three:
            case ResourceTable.Id_four:
                int argb = Color.argb(COLOR_ALPHA, COLOR_RED, COLOR_GREEN, COLOR_BLUE);
                Color color = new Color(argb);
                new StyleableToast
                    .Builder(this)
                    .text("Element clicked")
                    .textColor(Color.BLACK)
                    .textSize(TEXT_SIZE)
                    .cornerRadius(CORNER_RADIUS)
                    .backgroundColor(color)
                    .show();

                break;
            default:
                break;
        }
    }
}
